package cn.jucheng.www.yizhudan.widget;

import android.os.Bundle;

import java.util.Timer;
import java.util.TimerTask;

import cn.jucheng.jclibs.socket.MyGlobal;
import cn.jucheng.jclibs.socket.WorkService;
import cn.jucheng.jclibs.tools.DataUtils;
import cn.jucheng.jclibs.tools.MyLog;


/**
 * 通信协议
 */
public class MyMessage {
    private static final String TAG = "MyMessage";
    // TODO 常量
    public static final String D_XITONGBIAOSHI = "4F";
    public static final String D_BANBENHAO = "0100";
    /**
     * 本程序
     */
    public static final String D_SHEBEI_RUANJIAN = "6832";
    /**
     * 对方，和我们通信的那一方
     */
    public static final String D_SHEBEI_DUIFANG = "6831";
    /**
     * 命令字-心跳检测
     */
    public static final int MLZ_XINTIAOJIANCE = 0x00A5;
    /**
     * 命令字-备案
     */
    public static final int MLZ_BEIAN = 0x00A6;
    /**
     * 命令字-设备状态及电量：0X00A0
     */
    public static final int MLZ_SHEBEIZHUANGTAI_A0 = 0x00A0;
    /**
     * 命令字-设备状态及电量：0X00AD
     */
    public static final int MLZ_SHEBEIZHUANGTAI_AD = 0x00AD;
    /**
     * 命令字-应答设备的状态：0X00AD
     */
    public static final int MLZ_YINGDASHEBEIZHUANGTAI_AD = 0xAD;
    /**
     * 命令字-校正设备时间
     */
    public static final int MLZ_JIAOZHENGSHEBEISHIJIAN = 0x01A0;
    /**
     * 命令字-病人腕带信息
     */
    public static final int MLZ_BINGRENWANDAIXINXI = 0x0D11;
    /**
     * 命令字-学生填写的各种表单
     */
    public static final int MLZ_BIAODAN = 0x0D12;
    /**
     * 命令字-更改医嘱状态
     */
    public static final int MLZ_GENGGAIYIZHUZHUANGTAI = 0x0D14;
    /**
     * 命令字-表单头信息
     */
    public static final int MLZ_BIAODANYOUXINXI = 0x0D16;
    /**
     * 命令字-清空消息
     */
    public static final int MLZ_CLEARALL = 0x0D19;






    // TODO 发送的数据
    /**
     * 心跳检测
     */
    private static final String MSG_XINTIAOJIANCE = "4F6832 0100 016831 0000000000000000000000000000 00A5 0000 00";
    /**
     * 设备备案
     */
    private static final String MSG_BEIAN = "4F6832 0100 016831 0000000000000000000000000000 00A6 0000 00";
    /**
     * 通知已扫描病人腕带二维码
     */
    private static final String MSG_SAOMIAOBINGRENWANDAI = "4F6832 0100 016831 0000000000000000000000000000 0D10 0000 00";
    /**
     * 学生点击签字
     */
    private static final String MSG_XUESHENGDIANJIQIANZI = "4F6832 0100 016831 0000000000000000000000000000 0D13 0003 %02x%04x 00";
    /**
     * 通知已扫描病人药物二维码
     */
    private static final String MSG_SAOMIAOBINGRENYAOWU = "4F6832 0100 016831 0000000000000000000000000000 0D15 0002 %04x 00";
    /**
     * 发送设备状态以及电量
     */
    private static final String MSG_SHEBEIZHUANGTAI = "453433 0100 013430 0000000000000000000000000000 00A0 0003 %02x%02x%02x 00";

    // TODO 用于测试的接收的数据
    /**
     * 设备状态及电量：0X00A0
     */
    private static final String MSG_TEST_SHEBEIZHUANGTAI_A0 = "453433 0100 013430 0000000000000000000000000000 00A0 0003 %02x%02x 00";
    /**
     * 应答设备状态及电量：0X00AD
     */
    private static final String MSG_TEST_SHEBEIZHUANGTAI_AD = "4F6831 0100 016832 0000000000000000000000000000 00AD 0003 %02x%02x%02x 00";
    /**
     * 校正设备时间
     */
    private static final String MSG_TEST_JIAOZHENGSHEBEISHIJIAN = "4F6831 0100 016832 0000000000000000000000000000 01A0 0007 %04x%02x%02x%02x%02x%02x 00";
    /**
     * 病人腕带信息
     */
    private static final String MSG_TEST_BINGRENWANDAIXINXI = "4F6831 0100 016832 0000000000000000000000000000 0D11 %04x ";
    /**
     * 学生填写的各种表单
     */
    private static final String MSG_TEST_BIAODAN = "4F6831 0100 016832 0000000000000000000000000000 0D12 %04x %02x%02x%04x%02x%04x ";
    /**
     * 更改医嘱状态
     */
    private static final String MSG_TEST_GENGGAIYIZHUZHUANGTAI = "4F6831 0100 016832 0000000000000000000000000000 0D14 0005 %02x%02x%04x%02x 00";

    /**
     * @return MSG_XINTIAOJIANCE
     */
    public static String getMsgXintiaojiance() {
        return MSG_XINTIAOJIANCE;
    }


    /**
     * @return MSG_BEIAN
     */
    public static String getMsgBeian() {
        return MSG_BEIAN;
    }

    /**
     * @return MSG_SAOMIAOBINGRENWANDAI
     */
    public static String getMsgSaomiaobingrenwandai() {
        return MSG_SAOMIAOBINGRENWANDAI;
    }

    /**
     * @return MSG_SAOMIAOBINGRENYAOWU
     */
    public static String getMsgSaomiaobingrenyaowu(int drug_id) {
        return String.format(MSG_SAOMIAOBINGRENYAOWU, drug_id);
    }

    /**
     * @return MSG_XUESHENGDIANJIQIANZI
     */
    public static String getMsgXueshengdianjiqianzi(int type, int rowNum) {
        return String.format(MSG_XUESHENGDIANJIQIANZI, type, rowNum);
    }

    /**
     * @return MSG_SHEBEIZHUANGTAI
     */
    public static String getMsgXueshengzhuangtaidianliang(int bianhao, int type, int levenumber) {
        return String.format(MSG_SHEBEIZHUANGTAI, bianhao, type, levenumber);
    }


    public static String getMsgTestShebeizhuangtaiA0(int bianHao, int zhuangTai) {
        return String.format(MSG_TEST_SHEBEIZHUANGTAI_A0, bianHao, zhuangTai);
    }

    public static String getMsgTestShebeizhuangtaiAd(int zhuangTai,
                                                     int diaoLianXian) {
        return String.format(MSG_TEST_SHEBEIZHUANGTAI_AD, zhuangTai, 10,
                diaoLianXian);
    }

    public static String getMsgTestJiaozhengshebeishijian(int nian, int yue,
                                                          int ri, int shi, int fen, int miao) {
        return String.format(MSG_TEST_JIAOZHENGSHEBEISHIJIAN, nian, yue, ri,
                shi, fen, miao);
    }

    public static String getMsgTestBingrenwandaixinxi(String xingMing,
                                                      String xingBie, String nianLing, String zhuYuanHao,
                                                      String chuangHao, String keShi, String guoMinShi, String zhenDuan) {
        String xingMing2 = HexadecimalConver.getHexResult(xingMing);
        int xingMingLen = xingMing2.length() / 2;
        String xingBie2 = HexadecimalConver.getHexResult(xingBie);
        int xingBieLen = xingBie2.length() / 2;
        String nianLing2 = HexadecimalConver.getHexResult(nianLing);
        int nianLingLen = nianLing2.length() / 2;
        String zhuYuanHao2 = HexadecimalConver.getHexResult(zhuYuanHao);
        int zhuYuanHaoLen = zhuYuanHao2.length() / 2;
        String chuangHao2 = HexadecimalConver.getHexResult(chuangHao);
        int chuangHaoLen = chuangHao2.length() / 2;
        String keShi2 = HexadecimalConver.getHexResult(keShi);
        int keShiLen = keShi2.length() / 2;
        String guoMinShi2 = HexadecimalConver.getHexResult(guoMinShi);
        int guoMinShiLen = guoMinShi2.length() / 2;
        String zhenDuan2 = HexadecimalConver.getHexResult(zhenDuan);
        int zhenDuanLen = zhenDuan2.length() / 2;
        return String.format(MSG_TEST_BINGRENWANDAIXINXI, xingMingLen
                + xingBieLen + nianLingLen + zhuYuanHaoLen + chuangHaoLen
                + keShiLen + guoMinShiLen + zhenDuanLen + 10)
                + String.format(" %02x ", xingMingLen)
                + xingMing2
                + String.format(" %02x ", xingBieLen)
                + xingBie2
                + String.format(" %02x ", nianLingLen)
                + nianLing2
                + String.format(" %02x ", zhuYuanHaoLen)
                + zhuYuanHao2
                + String.format(" %02x ", chuangHaoLen)
                + chuangHao2
                + String.format(" %02x ", keShiLen)
                + keShi2
                + String.format(" %04x ", guoMinShiLen)
                + guoMinShi2
                + String.format(" %04x ", zhenDuanLen) + zhenDuan2 + " 00";
    }

    public static String getMsgTestBiaodan(int biaoDanType, int yiZhuType,
                                           int yiZhuHangHao, int yiZhuZhuangTai, String yiZhu) {
        String yiZhu2 = HexadecimalConver.getHexResult(yiZhu);
        int len2 = yiZhu2.length() / 2;
        return String.format(MSG_TEST_BIAODAN, len2 + 7, biaoDanType,
                yiZhuType, yiZhuHangHao, yiZhuZhuangTai, len2) + yiZhu2 + "00";
    }

    public static String getMsgTestGenggaiyizhuzhuangtai(int yiZhuType,int zhonglei, int hangHao, int zhuangTai) {
        return String.format(MSG_TEST_GENGGAIYIZHUZHUANGTAI, yiZhuType,zhonglei, hangHao, zhuangTai,
                zhuangTai);
    }

    public static void sendMessage(String msg) {
        msg = msg.replace(" ", "");
        byte[] buf = DataUtils.HexStringToBytes(msg);
        // MyLog.d(TAG, "发送消息:" + msg);
        if (WorkService.workThread != null
                && WorkService.workThread.isConnected()) {
            Bundle data = new Bundle();
            data.putByteArray(MyGlobal.BYTESPARA1, buf);
            data.putInt(MyGlobal.INTPARA1, 0);
            data.putInt(MyGlobal.INTPARA2, buf.length);
            WorkService.workThread.handleCmd(MyGlobal.CMD_WRITE, data);
        } else {
            MyLog.e(TAG, "发送消息时未连接");
        }
    }

    public static void sendMessage(final String msg, long delay) {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                sendMessage(msg);
            }
        }, delay);
    }

}
